using Engine;
using Engine.Graphics;
using Game;

namespace StyleSurvivalcraft;

public abstract class StyleCrossBlocks : CrossBlock
{
    public Texture2D m_texture;
    public string m_textureRoute;

    public StyleCrossBlocks(string textureRoute)
    {
        m_textureRoute = textureRoute;
    }
    public override int GetTextureSlotCount(int value)
    {
        return 1;
    }
    public override int GetFaceTextureSlot(int face, int value)
    {
        return 0;
    }
    public override void Initialize()
    {
        m_texture = ContentManager.Get<Texture2D>(m_textureRoute);
    }
    public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
    {
        generator.GenerateCrossfaceVertices(this, value, x, y, z, Color.White, GetFaceTextureSlot(0, value), geometry.GetGeometry(m_texture).SubsetAlphaTest);
    }

    public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
    {
        color *= BlockColorsMap.Grass.Lookup(environmentData.Temperature, environmentData.Humidity);

        BlocksManager.DrawFlatOrImageExtrusionBlock(primitivesRenderer, value, size, ref matrix, m_texture, color, isEmissive: false, environmentData);
    }

}
public abstract class StyleSixFaceBlock : CubeBlock
{
	public Texture2D m_texture;

	public Color m_color;

	public StyleSixFaceBlock(Color color)
	{
		m_color = color;
	}

	public override void Initialize()
	{
		m_texture = ContentManager.Get<Texture2D>("Textures/Plant/StyleTreeMainBlock");
	}

	public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
	{
		generator.GenerateCubeVertices(this, value, x, y, z, m_color, geometry.GetGeometry(m_texture).OpaqueSubsetsByFace);
	}

	public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
    { 
	    BlocksManager.DrawCubeBlock(primitivesRenderer,value , new Vector3(size), 1f,ref matrix, color, color, environmentData,m_texture);
    }

	public override int GetTextureSlotCount(int value)
	{
		return 16;
	}

	public override int GetFaceTextureSlot(int face, int value)
	{
		switch (face)
		{
			case 0: return 0;//  右				
			case 1: return 1; //后
			case 2: return 2; //左
			case 3: return 3;//前
			case 4: return 4;//上
			case 5: return 5;//下
		}
		return 0;
	}
}

public class StyleSakuraBlock : StyleSixFaceBlock
{
	public static int Index = BlocksManager.GetBlockIndex<StyleSakuraBlock>();
	public StyleSakuraBlock() : base(Color.White)
	{
		DefaultCategory = "StyleSurvivalcraft";
		DefaultDisplayName = "Style Sakura Block";
		DefaultDescription = "Style Sakura Block";
		FirstPersonScale = 0.4f;
		DigMethod = BlockDigMethod.Hack;
		CraftingId = "wood";
		IsPlaceable = true;
		DefaultDropContent = BlocksManager.GetBlockIndex<StyleSakuraBlock>();
		DefaultDropCount = 1f;
		MaxStacking = 40;
		FuelFireDuration = 10;
		FuelHeatLevel = 1;
		DigResilience = 3;
	}
	public override int GetFaceTextureSlot(int face, int value)
	{
		switch (face)
		{
			case 0: return 1;//  右				
			case 1: return 1; //后
			case 2: return 1; //左
			case 3: return 1;//前
			case 4: return 2;//上
			case 5: return 2;//下
		}
		return 0;
	}
}

public class StyleSakuraLeavesBlock : StyleSixFaceBlock
{
	public static int Index = BlocksManager.GetBlockIndex<StyleSakuraLeavesBlock>();
	public StyleSakuraLeavesBlock() : base(Color.White)
	{
		DefaultCategory = "StyleSurvivalcraft";
		DefaultDisplayName = "Style Sakura Leaves Block";
		DefaultDescription = "Style Sakura Leaves Block";
		FirstPersonScale = 0.4f;
		DigMethod = BlockDigMethod.Hack;
		CraftingId = "leaves";
		IsPlaceable = true;
		IsPlacementTransparent = true;
		IsTransparent = true;
		DefaultDropContent = BlocksManager.GetBlockIndex<StyleSakuraLeavesBlock>();
		DefaultDropCount = 1f;
		MaxStacking = 40;
		FuelFireDuration = 5;
		DigResilience = 1;
		FuelHeatLevel = 1;
	}
	public override int GetFaceTextureSlot(int face, int value)
	{
		switch (face)
		{
			case 0: return 0;//右				
			case 1: return 0;//后
			case 2: return 0;//左
			case 3: return 0;//前
			case 4: return 0;//上
			case 5: return 0;//下
		}
		return 0;
	}
}

public class StyleCrossBlock : StyleCrossBlocks
{
	public static int Index = StyleCrossBlock.Index;
    public StyleCrossBlock() : base("Textures/Plant/StyleCrossBlock")
    {
        DefaultDisplayName = "Style Cross Block";
        DefaultDescription = "Style Cross Block";
        DefaultCategory = "StyleSurvivalcraft";
        CraftingId = "stylecross";
        FirstPersonScale = 0.4f;
        DigMethod = BlockDigMethod.Hack;
        IsCollidable = false;
        DigResilience = 0.01f;
        DefaultDropContent = BlocksManager.GetBlockIndex<StyleCrossBlock>();
        DefaultDropCount = 1.5f;
        IsGatherable = true;
        RequiredToolLevel = 0;
        FirstPersonOffset = new Vector3(0.5f, -0.5f, -0.6f);
        InHandScale = 0.3f;
        IsTransparent = true;
    }
}

public class StyleTreeBlock : StyleCrossBlocks
{
	public StyleTreeBlock()
	   : base("Textures/Plant/StyleTreeBlock")
	{
		DefaultCategory = "StyleSurvivalcraft";
		FirstPersonScale = 0.4f;
		DigMethod = BlockDigMethod.Hack;
		IsCollidable = false;
		DigResilience = 0.01f;
		IsGatherable = true;
		RequiredToolLevel = 0;
		FirstPersonOffset = new Vector3(0.5f, -0.5f, -0.6f);
		InHandScale = 0.3f;
		IsTransparent = true;
	}
	public override int GetTextureSlotCount(int value)
	{
		return 3;
	}
	public override BlockDebrisParticleSystem CreateDebrisParticleSystem(SubsystemTerrain subsystemTerrain, Vector3 position, int value, float strength)
	{
		return new BlockDebrisParticleSystem(subsystemTerrain, position, strength, DestructionDebrisScale, new Color(144, 238, 144), GetFaceTextureSlot(0, value));
	}
	public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
	{
		BlocksManager.DrawFlatOrImageExtrusionBlock(primitivesRenderer, value, size, ref matrix, m_texture, color, isEmissive: false, environmentData);
	}
	public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
	{
		generator.GenerateCrossfaceVertices(this, value, x, y, z, Color.White, GetFaceTextureSlot(0, value), geometry.GetGeometry(m_texture).SubsetAlphaTest);
	}
	public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
	{
		switch (Terrain.ExtractData(value))
		{
			case 0:
				return "柠檬树树苗";
			case 1:
				return "桃树苗";
			case 2:
				return "幽静花树苗";
			case 3:
				return "樱花树幼苗";
			case 4:
				return "橡胶树苗";
			default:
				return "树苗";
		}
	}
	public override string GetDescription(int value)
	{
	    int data = Terrain.ExtractData(value);
	    string result;
	    if (LanguageControl.TryGetBlock(string.Format("{0}:{1}", (object)this.GetType().Name, (object)data), "Description", out result))
	    {
	        return result;
	    }
	    else
	    {
	        switch (data)
	        {
	            case 0:
	                return "这是一棵柠檬树树苗";
	            case 1:
	                return "这是一棵桃树苗";
	            case 2:
	                return "这是一棵幽静花树苗";
	            case 3:
	                return "樱花树幼苗";
	            case 4:
	                return "这是一棵橡胶树苗";
	            default:
	                return this.DefaultDescription; 
	        }
	    }
	}

	public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
	{
		showDebris = true;
		dropValues.Add(new BlockDropValue
		{
			Value = oldValue,
			Count = 1
		});
	}

	public override int GetFaceTextureSlot(int face, int value)
	{
		switch (Terrain.ExtractData(value))
		{
			case 0:
				return 0;
			case 1:
				return 1;
			case 2:
				return 2;
			case 3:
				return 3;
			case 4:
				return 4;
			default:
				return 0;
		}
	}
	public override IEnumerable<int> GetCreativeValues()
	{
		yield return Terrain.MakeBlockValue(BlocksManager.GetBlockIndex<StyleTreeBlock>(), 0, 0);
		yield return Terrain.MakeBlockValue(BlocksManager.GetBlockIndex<StyleTreeBlock>(), 0, 1);
		yield return Terrain.MakeBlockValue(BlocksManager.GetBlockIndex<StyleTreeBlock>(), 0, 2);
		yield return Terrain.MakeBlockValue(BlocksManager.GetBlockIndex<StyleTreeBlock>(), 0, 3);
		yield return Terrain.MakeBlockValue(BlocksManager.GetBlockIndex<StyleTreeBlock>(), 0, 4);
		yield break;
	}
	public static int Index = BlocksManager.GetBlockIndex<StyleTreeBlock>();
}

    